package com.hckjsec.website.controller.webpublic;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hckjsec.common.constant.HttpStatus;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.core.domain.BaseEntity;
import com.hckjsec.common.core.page.TableDataInfo;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.website.domain.*;
import com.hckjsec.website.domain.view.*;
import com.hckjsec.website.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 此类处理网站端请求的接口数据
 */
@RestController
@RequestMapping("/website/api")
public class WebSitePublicApiController   extends BaseController  {
    @Autowired
    private IWebSiteConfigService webSiteConfigService;
    @Autowired
    private IWebSiteNewsService webSiteNewsService;
    @Autowired
    private IWebSiteNotifyService webSiteNotifyService;
    @Autowired
    private IWebSiteSpecialService webSiteSpecialService;
    @Autowired
    private IWebSiteLawstatuteService webSiteLawstatuteService;
    @Autowired
    private IWebSiteGuideService webSiteGuideService;
    @Autowired
    private IWebSiteTeamService webSiteTeamService;
    @Autowired
    private IWebSiteLinksService webSiteLinksService;
    @Autowired
    private IWebSiteMechanismService webSiteMechanismService;
    @Autowired
    private IWebSiteLearningService webSiteLearningService;
    @Autowired
    private IWebSiteMatterService webSiteMatterService;
    @Autowired
    private IWebSiteMatterTableService webSiteMatterTableService;
    @Autowired
    private IWebSiteCheckedroomService webSiteCheckedroomService;
    //网站端初始化
    @PostMapping("/global")
    public AjaxResult global(@RequestBody WebSiteView webSiteView){
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("appId不能为空");
        }
        WebSiteHomeData webSiteHomeData = new WebSiteHomeData();
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        if (StringUtils.isNull(webSiteConfig)){
            return AjaxResult.cloud_602("未配置信息");
        }
        webSiteHomeData.setWebSiteConfig(webSiteConfig);
        Long deptId = webSiteConfig.getDeptId();
        //新闻动态
        List<WebSiteNewsData> newsDataList = new ArrayList<>();
        WebSiteNews topWebSiteNews = new WebSiteNews(deptId,"true","true");
        List<WebSiteNews> topNewsList = webSiteNewsService.selectWebSiteNewsList(topWebSiteNews);
        topNewsList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("news");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getTitle());
            webSiteNewsData.setImg(kv.getTitleImg());
            webSiteNewsData.setTime(kv.getPublishTime());
            newsDataList.add(webSiteNewsData);
        });
        topWebSiteNews.setTop("false");
        List<WebSiteNews> newsList = webSiteNewsService.selectWebSiteNewsList(topWebSiteNews);
        newsList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("news");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getTitle());
            webSiteNewsData.setImg(kv.getTitleImg());
            webSiteNewsData.setTime(kv.getPublishTime());
            newsDataList.add(webSiteNewsData);
        });
        webSiteHomeData.setNewsList(newsDataList);
        //通知公告
        List<WebSiteNewsData> notifyDataList = new ArrayList<>();
        WebSiteNotify webSiteNotify = new WebSiteNotify(deptId,"true");
        List<WebSiteNotify> notifyList = webSiteNotifyService.selectWebSiteNotifyList(webSiteNotify);
        notifyList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("notify");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getTitle());
            webSiteNewsData.setImg(kv.getTitleImg());
            webSiteNewsData.setTime(kv.getPublishTime());
            notifyDataList.add(webSiteNewsData);
        });
        webSiteHomeData.setNotifyList(notifyDataList);
        //专项专题 - 浮动
        List<WebSiteSpecialHomeData> floatSpecialDataList = new  ArrayList<>();
        WebSiteSpecial webSiteSpecial = new WebSiteSpecial(deptId,"true","true");
        List<WebSiteSpecial> floatSpecialList = webSiteSpecialService.selectWebSiteSpecialList(webSiteSpecial);
        floatSpecialList.forEach(kv->{
            WebSiteSpecialHomeData webSiteSpecialHomeData = new  WebSiteSpecialHomeData();
            webSiteSpecialHomeData.setName(kv.getName());
            webSiteSpecialHomeData.setImg(kv.getImg());
            webSiteSpecialHomeData.setUrl(kv.getUrl());
            webSiteSpecialHomeData.setTime(kv.getOperatortime());
            floatSpecialDataList.add(webSiteSpecialHomeData);
        });
        webSiteHomeData.setFloatSpecialList(floatSpecialDataList);
        //专项专题
        List<WebSiteSpecialHomeData> specialDataList = new  ArrayList<>();
        webSiteSpecial.setIsFloat("false");
        List<WebSiteSpecial> SpecialList = webSiteSpecialService.selectWebSiteSpecialList(webSiteSpecial);
        SpecialList.forEach(kv->{
            WebSiteSpecialHomeData webSiteSpecialHomeData = new  WebSiteSpecialHomeData();
            webSiteSpecialHomeData.setName(kv.getName());
            webSiteSpecialHomeData.setImg(kv.getImg());
            webSiteSpecialHomeData.setUrl(kv.getUrl());
            webSiteSpecialHomeData.setTime(kv.getOperatortime());
            specialDataList.add(webSiteSpecialHomeData);
        });
        webSiteHomeData.setSpecialList(specialDataList);
        //法律法规
        List<WebSiteNewsData> lawstatuteDataList = new  ArrayList<>();
        WebSiteLawstatute webSiteLawstatute = new WebSiteLawstatute(deptId,"true");
        List<WebSiteLawstatute> lawstatuteList = webSiteLawstatuteService.selectWebSiteLawstatuteList(webSiteLawstatute);
        lawstatuteList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("lawstatute");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getTitle());
            webSiteNewsData.setTime(kv.getPublishTime());
            lawstatuteDataList.add(webSiteNewsData);
        });
        webSiteHomeData.setLawstatuteList(lawstatuteDataList);
        //办证指南
        List<WebSiteNewsData> guideDataList = new  ArrayList<>();
        WebSiteGuide webSiteGuide = new WebSiteGuide(deptId,"true");
        List<WebSiteGuide> guideList = webSiteGuideService.selectWebSiteGuideList(webSiteGuide);
        guideList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("lawstatute");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getTitle());
            webSiteNewsData.setTime(kv.getPublishTime());
            guideDataList.add(webSiteNewsData);
        });
        webSiteHomeData.setGuideList(guideDataList);
        //团队介绍
        List<WebSiteTeamData> teamDataList = new  ArrayList<>();
        WebSiteTeam webSiteTeam = new WebSiteTeam(deptId,"true");
        List<WebSiteTeam> teamList = webSiteTeamService.selectWebSiteTeamList(webSiteTeam);
        teamList.forEach(kv->{
            WebSiteTeamData webSiteTeamData = new WebSiteTeamData("team");
            webSiteTeamData.setId(kv.getId());
            webSiteTeamData.setName(kv.getName());
            webSiteTeamData.setImg(kv.getNameImg());
            webSiteTeamData.setDuties(kv.getDuties());
            teamDataList.add(webSiteTeamData);
        });
        webSiteHomeData.setTeamList(teamDataList);
        //友情链接
        List<WebSiteSpecialHomeData> linksDataList = new  ArrayList<>();
        WebSiteLinks webSiteLinks = new WebSiteLinks(deptId,"true");
        List<WebSiteLinks> linksList = webSiteLinksService.selectWebSiteLinksList(webSiteLinks);
        linksList.forEach(kv->{
            WebSiteSpecialHomeData webSiteSpecialHomeData = new  WebSiteSpecialHomeData();
            webSiteSpecialHomeData.setName(kv.getName());
            webSiteSpecialHomeData.setUrl(kv.getUrl());
            webSiteSpecialHomeData.setTime(kv.getOperatortime());
            linksDataList.add(webSiteSpecialHomeData);
        });
        webSiteHomeData.setLinksList(linksDataList);
        //学术研究
        WebSiteLearning webSiteLearning = new WebSiteLearning(deptId,"true");
        List<WebSiteLearning> webSiteLearningList = webSiteLearningService.selectWebSiteLearningList(webSiteLearning);
        webSiteHomeData.setWebSiteLearning(webSiteLearningList);
        //材料信息
        List<WebSiteNewsData> matterDataList = new  ArrayList<>();
        WebSiteMatter webSiteMatter = new WebSiteMatter(deptId,"true");
        List<WebSiteMatter> matterList = webSiteMatterService.selectWebSiteMatterList(webSiteMatter);
        matterList.forEach(kv->{
            WebSiteNewsData webSiteNewsData = new WebSiteNewsData("matter");
            webSiteNewsData.setId(kv.getId());
            webSiteNewsData.setTitle(kv.getMatterName());
            webSiteNewsData.setImg(kv.getNameIcon());
            matterDataList.add(webSiteNewsData);
        });
        webSiteHomeData.setMatterList(matterDataList);
        return AjaxResult.success(webSiteHomeData);
    }

    //获取机构简介信息
    @PostMapping("/getMechanism")
    public AjaxResult getIntroduce(@RequestBody WebSiteView webSiteView){
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("appId不能为空");
        }
        String type = StringUtils.isHcNull(webSiteView.getType(),"");
        if("".equals(type)){
            return AjaxResult.cloud_602("type不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        WebSiteMechanism  mechanismData = new WebSiteMechanism();
        WebSiteMechanism webSiteMechanism = new WebSiteMechanism();
        webSiteMechanism.setDeptId(deptId);
        webSiteMechanism.setType(type);
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteMechanism);
        if (!StringUtils.isEmpty(list)){
            mechanismData = list.get(0);
        }
        return AjaxResult.success(mechanismData);
    }
    protected TableDataInfo getDataErrorTable(String msg)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.HTTP_602);
        rspData.setMsg(msg);
        rspData.setRows(null);
        rspData.setTotal(0);
        return rspData;
    }

    //工作动态
    @PostMapping("/getNewsList")
    public TableDataInfo getNewsList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteNews webSiteNews = new WebSiteNews(deptId,"true");
        List<WebSiteNews> list = webSiteNewsService.selectWebSiteNewsList(webSiteNews);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }


    //公证云选房
    @PostMapping("/getCheckedroomList")
    public TableDataInfo getCheckedroomList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteCheckedroom webSiteCheckedroom = new WebSiteCheckedroom(deptId,"true");
        List<WebSiteCheckedroom> list = webSiteCheckedroomService.selectWebSiteCheckedroomList(webSiteCheckedroom);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }


    //通知公告
    @PostMapping("/getNotifyList")
    public TableDataInfo getNotifyList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteNotify webSiteNotify = new WebSiteNotify(deptId,"true");
        List<WebSiteNotify> list = webSiteNotifyService.selectWebSiteNotifyList(webSiteNotify);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    //人员风采
    @PostMapping("/getTeamList")
    public AjaxResult getTeamList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        WebSiteTeam webSiteTeam = new WebSiteTeam(deptId,"true");
        List<WebSiteTeam> list = webSiteTeamService.selectWebSiteTeamList(webSiteTeam);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return AjaxResult.success(list);
    }

    //法律法规
    @PostMapping("/getLawstatuteList")
    public TableDataInfo getLawstatuteList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteLawstatute webSiteLawstatute = new WebSiteLawstatute(deptId,"true");
        List<WebSiteLawstatute> list = webSiteLawstatuteService.selectWebSiteLawstatuteList(webSiteLawstatute);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    //办证指南
    @PostMapping("/getGuideList")
    public TableDataInfo getGuideList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteGuide webSiteGuide = new WebSiteGuide(deptId,"true");
        List<WebSiteGuide> list = webSiteGuideService.selectWebSiteGuideList(webSiteGuide);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }

    //学术研究
    @PostMapping("/getLearningList")
    public TableDataInfo getLearningList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return getDataErrorTable("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        startWebPage(webSiteView.getPageNum(),webSiteView.getPageSize());
        WebSiteLearning webSiteLearning = new WebSiteLearning(deptId,"true");
        List<WebSiteLearning> list = webSiteLearningService.selectWebSiteLearningList(webSiteLearning);
        list.forEach(kv->{
            kv.setContent(null);
        });
        return getDataTable(list);
    }


    //详情
    @PostMapping("/getDetails")
    public AjaxResult getDetails(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("appId不能为空");
        }
        String type = StringUtils.isHcNull(webSiteView.getType(),"");
        if("".equals(type)){
            return AjaxResult.cloud_602("type不能为空");
        }
        Object data = new Object();
        if ("news".equals(type.trim())){
            data = webSiteNewsService.selectWebSiteNewsById(webSiteView.getId());
        }else if("notify".equals(type.trim())){
            data = webSiteNotifyService.selectWebSiteNotifyById(webSiteView.getId());
        }else if("team".equals(type.trim())){
            data = webSiteTeamService.selectWebSiteTeamById(webSiteView.getId());
        }else if("lawstatute".equals(type.trim())){
            data = webSiteLawstatuteService.selectWebSiteLawstatuteById(webSiteView.getId());
        }else if("guide".equals(type.trim())){
            data = webSiteGuideService.selectWebSiteGuideById(webSiteView.getId());
        }else if("learning".equals(type.trim())){
            data = webSiteLearningService.selectWebSiteLearningById(webSiteView.getId());
        }else if("checkedroom".equals(type.trim())){
            data = webSiteCheckedroomService.selectWebSiteCheckedroomById(webSiteView.getId());
        }else if("matter".equals(type.trim())){
            WebSiteMatter webSiteMatter = webSiteMatterService.selectWebSiteMatterById(webSiteView.getId());
            List<WebSiteMatterTableView> tableList = new ArrayList<>();
            Long mattId = webSiteMatter.getId();
            WebSiteMatterTable webSiteMatterTable = new WebSiteMatterTable(mattId);
            List<WebSiteMatterTable> list = webSiteMatterTableService.selectWebSiteMatterTableList(webSiteMatterTable);
            list.forEach(kv->{
                WebSiteMatterTableView webSiteMatterTableView = new WebSiteMatterTableView();
                webSiteMatterTableView.setName(kv.getTableName());
                webSiteMatterTableView.setUrl(kv.getNamePath());
                tableList.add(webSiteMatterTableView);
            });
            webSiteMatter.setTableList(tableList);
            data = webSiteMatter;
        }
        return AjaxResult.success(data);
    }

    public static void main(String[] args) throws Exception{
        WebSiteNews     webSiteNews  = new  WebSiteNews();
        webSiteNews.setDeptId(100L);
        webSiteNews.setContent("111");
        System.out.println(ComUtil.isExistField("id",webSiteNews));
        System.out.println(ComUtil.getFieldValueByName("content",webSiteNews));
    }

    //事项列表
    @PostMapping("/getMatterList")
    public AjaxResult getMatterList(@RequestBody WebSiteView webSiteView)
    {
        String appid = StringUtils.isHcNull(webSiteView.getAppid(),"");
        if("".equals(appid)){
            return AjaxResult.cloud_602("appId不能为空");
        }
        WebSiteConfig webSiteConfig = webSiteConfigService.selectWebSiteConfigById(Long.valueOf(appid));
        Long deptId = webSiteConfig.getDeptId();
        WebSiteMatter webSiteMatter = new WebSiteMatter(deptId,"true");
        List<WebSiteMatter> matterList = webSiteMatterService.selectWebSiteMatterList(webSiteMatter);
        matterList.forEach(kv->{
            List<WebSiteMatterTableView> tableList = new ArrayList<>();
            Long mattId = kv.getId();
            WebSiteMatterTable webSiteMatterTable = new WebSiteMatterTable(mattId);
            List<WebSiteMatterTable> tableList1 = webSiteMatterTableService.selectWebSiteMatterTableList(webSiteMatterTable);
            tableList1.forEach(kvTable->{
                WebSiteMatterTableView webSiteMatterTableView = new WebSiteMatterTableView();
                webSiteMatterTableView.setName(kvTable.getTableName());
                webSiteMatterTableView.setUrl(kvTable.getNamePath());
                tableList.add(webSiteMatterTableView);
            });
            kv.setTableList(tableList);
        });
        return AjaxResult.success(matterList);
    }
}
